package com.cy.ppj.web.front.user;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.domian.user.User;
import com.cy.ppj.model.query.message.MessageRecordQuery;
import com.cy.ppj.model.query.productannouncement.ProductAnnouncementQuery;
import com.cy.ppj.model.vo.front.message.MessageIndexVO;
import com.cy.ppj.model.vo.front.message.MessageRecordVO;
import com.cy.ppj.model.vo.front.productannouncement.ProductAnnouncementVO;
import com.cy.ppj.service.message.MessageRecordService;
import com.cy.ppj.service.productannouncement.ProductAnnouncementService;
import com.cy.ppj.service.user.UserService;
import com.cy.ppj.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 前台-用户-消息
 *
 * @author: tanyilin
 * @create: 2018年9月18日 下午2:47:29
 */
@Slf4j
@RestController
@RequestMapping("/user/message")
@Api(tags = "前台-用户-消息")
public class UserMessageController extends BaseController{

	@Autowired
	private MessageRecordService messageRecordService;
	@Autowired
	private ProductAnnouncementService productAnnouncementService;
	@Autowired
	private UserService userService;
	
	@ApiOperation("消息中心首页")
	@GetMapping("/index")
	public Result<List<MessageIndexVO>> Index() {
		List<MessageIndexVO> list = messageRecordService.selectMessageIndexVOs(this.getUser().getUserId());
		return ok("", list);
	}
	
	@ApiOperation("获取个人消息列表")
	@GetMapping("/personal/message/list")
	public PageBean<MessageRecordVO> personalMessageList(MessageRecordQuery query) {
		query.setUserId(this.getUser().getUserId());
		PageBean<MessageRecordVO> pageBean = messageRecordService.selectMessageRecordList(query);
		return pageBean;
	}
	
	@ApiOperation("产品或公告列表")
	@GetMapping("/product/announcement/list")
	public PageBean<ProductAnnouncementVO> list(@Validated ProductAnnouncementQuery query, BindingResult bindingResult) {
		this.checkError(bindingResult);
		PageBean<ProductAnnouncementVO> pageBean = productAnnouncementService.selectProductAnnouncement(query);
		return pageBean;
	}
	
	@ApiOperation("消息置为已读状态")
	@PostMapping("/do/read")
	public Result doRead(@ApiParam(value = "类型", example = "1") @RequestParam Integer type) {
		Long userId = this.getUser().getUserId();
		if(1 == type) {
			// 个人消息
			messageRecordService.updateToReadByUserId(userId);
		}else if(2 == type) {
			// 产品通知
			User user = new User();
			user.setId(userId);
			user.setLastReadProductNoticeTime(new Date());
			userService.updateUser(user);
		}else if(3 == type) {
			// 平台公告
			User user = new User();
			user.setId(userId);
			user.setLastReadPlatformAnnouncementTime(new Date());
			userService.updateUser(user);
		}
		
		return ok();
	}
	
}
